﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using MySql.Data.MySqlClient;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using System.Data;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_PS_AddStrategyPlan : System.Web.UI.Page
    {
        private IStrategyPlanBLL bal = new StrategyPlanBLL();
        private int Year;
        private Boolean isRoot = false;
        private string AddType;
        private int StrategyDataSeq;
        private string StrategyName;

        void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //btnAddData.Attributes.Add("OnClick", "javascript:Close(); return false;");
                Response.CacheControl = "no-cache";
                try
                {
                    if (Request.QueryString.Count > 0)
                    {
                        Year = Convert.ToInt16(Request.QueryString["year"].ToString());
                        AddType = Request.QueryString["add_type"].ToString();
                        if (AddType != "F_Root" && AddType != "E")
                        {
                            //StrategyType = Convert.ToInt16(Request.QueryString["strat_type"].ToString());
                            StrategyDataSeq = Convert.ToInt16(Request.QueryString["strat_type"].ToString());
                            StrategyName = Request.QueryString["strat_name"].ToString();
                        } else if(AddType == "E")
                        {
                            StrategyDataSeq = bal.GetRootStrategyTypeSeq();
                            StrategyName = "";
                        }
                        else
                        {
                            StrategyDataSeq = bal.GetRootStrategyTypeSeq();
                            StrategyName = "";
                        }

                    }
                    tView.Nodes.Clear();
                    lbYear.Text = Year.ToString();
                    if (AddType == "P" || AddType == "F_Root" || AddType == "E")
                    {
                        cbbGetDataStrategyType(StrategyDataSeq, AddType);
                        cbbStrategyType.SelectedIndex = 0;
                        if (bal.CheckAddParent(StrategyDataSeq) == 0)
                        {
                            isRoot = true;
                        }
                        else
                        {
                            string[] tmp = bal.GetParentStrategyTypeName(StrategyDataSeq);
                            StrategyDataSeq = Convert.ToInt32(tmp[0]);
                            StrategyName = tmp[1];
                        }
                        if (!isRoot)
                            tView.Nodes.Add(new RadTreeNode(StrategyName, StrategyDataSeq.ToString()));
                        cbbGetdataStrategyDataList(Convert.ToInt32(cbbStrategyType.SelectedValue.ToString()));
                        cbbStrategyDataList.SelectedIndex = -1;
                    }
                    else
                    {
                        cbbGetDataStrategyType(StrategyDataSeq, AddType);
                        cbbStrategyType.SelectedIndex = 0;
                        if (bal.CheckAddChild(StrategyDataSeq) == 0)
                        {
                            Alert("ไม่สามารถ Add Child ได้");
                            tView.Nodes.Add(new RadTreeNode(StrategyName, StrategyDataSeq.ToString()));
                        }
                        else
                        {
                            tView.Nodes.Add(new RadTreeNode(StrategyName, StrategyDataSeq.ToString()));
                            cbbGetdataStrategyDataList(Convert.ToInt32(cbbStrategyType.SelectedValue.ToString()));
                            cbbStrategyDataList.SelectedIndex = -1;
                        }
                    }
                }
                catch
                {
                    Alert("เกิดข้อผิดพลาด");
                }
                Session["Year"] = Year;
                Session["isRoot"] = isRoot;
                Session["AddType"] = AddType;
                Session["StrategyDataSeq"] = StrategyDataSeq;
                Session["StrategyName"] = StrategyName;
            }
            else
            {
                //Page PostBack
                Year = (int)Session["Year"];
                isRoot = (Boolean)Session["isRoot"];
                AddType = Session["AddType"].ToString();
                StrategyDataSeq = (int)Session["StrategyDataSeq"];
                StrategyName = Session["StrategyName"].ToString();
            }
        }

        private void cbbGetDataStrategyType(int StrategyDataSeq, string AddType)
        {
            DataTable dt = bal.GetStrategyTypeForCombobox(StrategyDataSeq, AddType);
            cbbStrategyType.DataSource = dt;
            cbbStrategyType.DataTextField = "bgm_strat_type_name";
            cbbStrategyType.DataValueField = "bgm_strat_type_seq";
            cbbStrategyType.DataBind();
        }

        private void cbbGetdataStrategyDataList(int StrategyType)
        {
            DataTable dt = bal.GetStrategyDataListForCombobox(StrategyType, this.Year);
            cbbStrategyDataList.DataSource = dt;
            cbbStrategyDataList.DataTextField = "bgm_strat_list_name";
            cbbStrategyDataList.DataValueField = "bgm_strat_list_seq";
            cbbStrategyDataList.DataBind();
            cbbStrategyDataList_SelectedIndexChanged(null, null);
        }

        protected void cbbStrategyType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cbbStrategyType.SelectedIndex > -1)
                cbbGetdataStrategyDataList(Convert.ToInt32(cbbStrategyType.SelectedValue));
        }

        protected void cbbStrategyDataList_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadExample();
        }

        private void LoadExample()
        {
            if (isRoot)
            {
                tView.Nodes.Clear();
                if (cbbStrategyDataList.SelectedIndex > -1)
                {

                    tView.Nodes.Add(new RadTreeNode(txtBgmStratListCode.Text + " : " + cbbStrategyType.SelectedItem.Text + " : " + cbbStrategyDataList.SelectedItem.Text, cbbStrategyDataList.SelectedValue.ToString()));
                    tView.DataBind();
                    tView.ExpandAllNodes();
                }
            }
            else
            {
                tView.Nodes[0].Nodes.Clear();
                if (cbbStrategyDataList.SelectedIndex > -1)
                {

                    tView.Nodes[0].Nodes.Add(new RadTreeNode(txtBgmStratListCode.Text + " : " + cbbStrategyType.SelectedItem.Text + " : " + cbbStrategyDataList.SelectedItem.Text, cbbStrategyDataList.SelectedValue.ToString()));
                    tView.DataBind();
                    tView.ExpandAllNodes();
                }
            }
        }

        private void Alert(string Message)
        {
            string js = "";
            js = "alert('" + Message + "');";
            if (ScriptManager.GetCurrent(base.Page).IsInAsyncPostBack)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", js, true);
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", js, true);
            }
        }

        protected void btnAddData_Click(object sender, EventArgs e)
        {
            int intEmpSeq = Convert.ToInt32(new SsMng().EmpSeq);

            if (cbbStrategyType.SelectedIndex == -1)
            {
                Alert("กรุณาเลือกประเภทยุทธศาสตร์");
                return;
            }
            else if (cbbStrategyDataList.SelectedIndex == -1)
            {
                Alert("กรุณาเลือกทะเบียนยุทธศาสตร์");
                return;
            }
            else if (txtBgmStratListCode.Text.Trim().Length == 0)
            {
                Alert("กรุณากรอกรหัสยุทธศาสตร์");
                return;
            }
            else if (this.AddType == "P" || this.AddType == "F_Root")
            {
                int? tmp = null;
                if (bal.Insert(Convert.ToInt32(cbbStrategyType.SelectedValue), Convert.ToInt32(cbbStrategyDataList.SelectedValue),
                    (this.AddType == "F_Root") ? tmp : this.StrategyDataSeq, this.Year, "W", txtBgmStratListCode.Text, intEmpSeq, DateTime.Now) == 1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "CloseWindow", "var oArg = new Object();oArg.isRefresh = true;window.frameElement.radWindow.close(oArg);", true);
                }
                else
                {
                    Alert("ไม่สามารถบันทึกข้อมูลได้");
                }
            }
            else
            {
                if (bal.Insert(Convert.ToInt32(cbbStrategyType.SelectedValue), Convert.ToInt32(cbbStrategyDataList.SelectedValue),
                    this.StrategyDataSeq, this.Year, "W", txtBgmStratListCode.Text, intEmpSeq, DateTime.Now) == 1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "CloseWindow", "window.frameElement.radWindow.close(true);", true);
                }
                else
                {
                    Alert("ไม่สามารถบันทึกข้อมูลได้");
                }
            }
        }

        protected void txtBgmStratListCode_TextChanged(object sender, EventArgs e)
        {
            LoadExample();
        }
    }
}
